// pages/house/RentalHouseDetail/com/manage/manage.js
import api from '../../../../../common/api'
import util from '../../../../../utils/util'

const app = getApp();

Component({

  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/
  },

  /**
   * 组件的属性列表
   */
  properties: {
    detailContent: {
      type: Object,
      value: {}
    },
    vr: {
      type: Boolean,
      value: false
    },
    vrdown: {
      type: Boolean,
      value: false
    },
    bannerList: {
      type: Array,
      value: []
    },
    vrUrl: {
      type: String,
      value: ''
    },
    landlordList: {
      type: Array,
      value: []
    },
    houseTrackList: {
      type: Array,
      value: []
    },
    isTrackListMore: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    materialDisable: [],
    materialCurrent: [],
    materialList: [
      {
        id: 'DOWNLOAD_VR',
        icon: 'icon-xiazaiVR',
        title: '下载VR',
        color: '#5DD1F8',
      },
      {
        id: 'COMPOUND_PANORAMA',
        icon: 'icon-hechengquanjingtu',
        title: '合成全景图',
        color: '#3F79EC',
      },
      // {
      //   id: 'VR_TO_VIDEO',
      //   icon: 'icon-vrzhuanshipin',
      //   title: 'VR转视频',
      //   color: '#6294DF',
      // }, 
      {
        id: 'IMG_TO_VIDEO',
        icon: 'icon-vrzhuanshipin',
        title: '图片转视频',
        color: '#6294DF',
      },
      {
        id: 'COPY_VR',
        icon: 'icon-fuzhivrlianjie',
        title: '复制VR链接',
        color: '#70C7B8',
      },
      // {
      //   id: 'VIDEO_VOICE',
      //   icon: 'icon-shipinpeiyin',
      //   title: '视频配音',
      //   color: '#3F9FEC',
      // }, 
      {
        id: 'DOWNLOAD_PANORAMA',
        icon: 'icon-xiazaiquanjingtu',
        title: '下载全景图',
        color: '#4688DE',
      }, {
        id: 'DOWNLOAD_VIDEO',
        icon: 'icon-xiazaishipin',
        title: '下载视频',
        color: '#8C7AF8',
      }, {
        id: 'DOWNLOAD_IMG',
        icon: 'icon-xiazaitupian',
        title: '下载图片',
        color: '#5DA2F8',
      }, 
    ],
    controlCurrent: [],
    controlList: [{
      id: 'edit',
      icon: 'icon-xiugai',
      title: '修改',
      color: '#6294DF',
    }, {
      id: 'down',
      icon: 'icon-xiajia1',
      title: '下架',
      color: '#5DA2F8',
    }, {
      id: 'up',
      icon: 'icon-shangjia1',
      title: '上架',
      color: '#DD84FB',
    }, {
      id: 'del',
      icon: 'icon-laji',
      title: '删除',
      color: '#A4AEBB',
    }],
  },

  /**
   * 组件的数据监听
   */
  observers: {
    detailContent(val) {
      app.globalData.loginPromise.then(() => {
        if (!val) return;
        this.deployControlOptions()
        this.deployMaterialOptions()
      })
    },
    vrUrl(val) {
      this.deployMaterialOptions()
    }
  },

  /**
   * 组件的生命周期
   */
  lifetimes: {
    attached: function () {
      app.globalData.loginPromise.then(() => {})
    },
    detached: function () {},
  },

  /**
   * 页面的生命周期
   */
  pageLifetimes: {
    show: function () {

    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function (size) {
      // 页面尺寸变化
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 配置当前展示的操作
    deployControlOptions() {
      const {
        detailContent
      } = this.data
      if (detailContent.houDelVO.state === 'UNAVAILABLE') {
        this.setData({
          controlCurrent: ['edit', 'del', 'up']
        })
      } else {
        this.setData({
          controlCurrent: ['edit', 'del', 'down']
        })
      }
    },

    // 配置当前展示的素材
    deployMaterialOptions() {
      const {
        vrdown,
        vr,
        detailContent,
        bannerList,
        vrUrl
      } = this.data
      let materialDisable = [];
      let materialCurrent = [];
      if (!detailContent) return
      if (vr && detailContent.hmDelSummaryInfo.houSubTypeState != 1) {
        materialCurrent.push('COPY_VR')
      }
      // if (vr && detailContent.hmDelSummaryInfo.videoMaterial) {
      //   materialCurrent.push('VIDEO_VOICE')
      // }
      if (vrdown) {
        materialCurrent.push('DOWNLOAD_VR')
      }


      if ((vr && detailContent.hmDelSummaryInfo.houSubTypeState != 10) ||
        (vrdown && detailContent.hmDelSummaryInfo.houSubTypeState != 10) ||
        (detailContent.hmDelSummaryInfo.houSubTypeState == 10 && detailContent.hmDelSummaryInfo.availableScenesNum > 0)
      ) {
        materialCurrent.push('COMPOUND_PANORAMA')
        if (detailContent.hmDelSummaryInfo.houSubTypeState != 10) {
          materialDisable.push('DOWNLOAD_PANORAMA')
          materialDisable.push('COMPOUND_PANORAMA')
        }
      }
      // if (vrdown || vr) {
      //   materialCurrent.push('VR_TO_VIDEO')
      //   if (detailContent.hmDelSummaryInfo.houSubTypeState != 10) {
      //     materialDisable.push('VR_TO_VIDEO')
      //   }
      // }

      // 图片转视频
      if (detailContent.houImageVO && detailContent.houImageVO.length) {
        materialCurrent.push('IMG_TO_VIDEO')
      }

      if (vrdown || vr) {
        materialCurrent.push('DOWNLOAD_PANORAMA')
        if (!(detailContent.hmDelSummaryInfo.notAvailableScenesNum > 0)) {
          materialDisable.push('DOWNLOAD_PANORAMA')
        }
      }

      materialCurrent.push('DOWNLOAD_VIDEO')
      if (!vrUrl) {
        materialDisable.push('DOWNLOAD_VIDEO')
      }
      
      materialCurrent.push('DOWNLOAD_IMG')
      if (!(bannerList && bannerList.length)) {
        materialDisable.push('DOWNLOAD_IMG')
      }
      this.setData({
        materialCurrent,
        materialDisable
      })
    },

    onClickMaterial(e) {
      const {
        materialDisable
      } = this.data
      let target = e.currentTarget.dataset.target
      if (materialDisable.indexOf(target) !== -1) return
      this.triggerEvent('click', target)
    },

    onClickControl(e) {
      switch (e.currentTarget.dataset.target) {
        case 'edit':
          this.goRentalHouseModify()
          break;
        case 'up':
          this.houseReturn()
          break;
        case 'down':
          this.houseUndercarriage()
          break;
        case 'del':
          this.delHouse()
          break;
      }
    },

    // 跳转修改房源
    goRentalHouseModify() {
      wx.navigateTo({
        url: `/pages/addHouse/infoEntryHouse/infoEntryHouse?houDelId=${this.data.detailContent.houDelVO.id}&flag=0`,
      });
    },

    // 上架
    houseReturn() {
      const {
        houDelVO = {}
      } = this.data.detailContent || {}
      api.houseFail("", {
        delId: houDelVO.id,
        state: "AVAILABLE",
        houseId: houDelVO.houseId,
        invalidType: 1,
      }, (res) => {
        wx.showModal({
          content: "该房源已经重新上架是否立即更新？",
          cancelColor: "#ccc",
          confirmColor: "#f64e4e",
          success: (res) => {
            if (res.confirm) {
              this.goRentalHouseModify();
            }
          },
        });
      });
    },

    // 下架
    houseUndercarriage() {
      let delId = this.data.detailContent.houDelVO.id;
      let state = this.data.detailContent.houDelVO.state;
      let style = this.data.detailContent.houDelVO.delegationType;
      let houseId = this.data.detailContent.houDelVO.houseId;
      let delegationType = ''
      if (style == "SALES") {
        delegationType = "RENT";
      } else {
        delegationType = "SALES";
      }
      api.checkDelegationType({
        houseId: houseId,
        delegationType: delegationType,
      }, (res) => {
        if (res.data.content.length == 0) {
          wx.navigateTo({
            url: `/pages/house/houseUndercarriage/houseUndercarriage?delId=${delId}&state=${state}&style=${style}&houseId=${houseId}&invalidType=1`,
          });
        } else {
          wx.showModal({
            content: `该房源同时存在一套在${style == "SALES" ? "租" : "售"}房源，是否一起下架？`,
            cancelColor: "#ccc",
            confirmColor: "#f64e4e",
            cancelText: "否",
            confirmText: "是",
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: `/pages/house/houseUndercarriage/houseUndercarriage?delId=${delId}&state=${state}&style=both&invalidType=0&houseId=${houseId}`,
                });
              } else if (res.cancel) {
                wx.navigateTo({
                  url: `/pages/house/houseUndercarriage/houseUndercarriage?delId=${delId}&state=${state}&style=${style}&houseId=${houseId}&invalidType=1`,
                });
              }
            },
          });
        }
      });
    },

    // 删除房源
    delHouse() {
      let delId = this.data.detailContent.houDelVO.id || ''
      wx.showModal({
        title: '提示',
        content: '是否确认删除房源?',
        success: (res) => {
          if (res.confirm) {
            api.deleteHouseByIds({
              ids: [delId],
            }, (res) => {
              if (res && res.data && res.data.isSuccess && res.data.content && res.data.content.successCount >= 1) {
                wx.navigateBack({
                  delta: 1,
                })
              } else {
                if (res && res.data && res.data.content && res.data.content.errorDescription && res.data.content.errorDescription.length) {
                  this.delTips(res.data.content.errorDescription[0].code)
                } else {
                  wx.showToast({
                    title: '删除失败，请稍后再试~',
                    icon: 'none',
                  })
                }
              }
            }, () => {
              wx.showToast({
                title: '删除失败，请稍后再试~',
                icon: 'none',
              })
            });
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },

    // 删除提示
    delTips(code) {
      const options = {
        21001: '该房源图片下载中，无法删除该房源。',
        21002: '该房源合成视频中，无法删除该房源。',
        21003: '该房源vr下载中，无法删除该房源。',
        21004: '该房源全景图合成中，无法删除该房源。',
        21005: '该房源发房中，无法删除该房源。',
        21006: '该房源已发布合作房源不能删除。',
      }
      wx.showToast({
        icon: 'none',
        title: options[code] || '删除失败，请稍后再试~',
      })
    },

    // 添加房东
    onAddLandlord() {
      const {
        landlordList
      } = this.data
      if (landlordList && landlordList.length >= 3) {
        wx.showToast({
          icon: 'none',
          title: '最多添加3个号码',
        })
        return
      }
      this.triggerEvent('addLandlord')
    },

    // 删除房东
    onDelLandlord(e) {
      this.triggerEvent('delLandlord', e.detail)
    },

    // 编辑房东
    onEditLandlord(e) {
      this.triggerEvent('editLandlord', e.detail)
    },

    // 添加房源轨迹
    onAddHouseTrack() {
      this.triggerEvent('addHouseTrack')
    },

    // 点击房源轨迹加载更多
    onLoadMore() {
      this.triggerEvent('moreHouseTrack')
    }

  }
})